package com.tjbklx1.redis.test1;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@SuppressWarnings("deprecation")
public class RedisApi {

	private static JedisPool pool = null;

	public synchronized static JedisPool getPool() {
		if (pool == null) {
//			pool = new JedisPool(null,"localhost", 6379,100000,null,4);
			pool = new JedisPool("localhost", 6379);
		}
		return pool;
	}

	/**
	 * 返还到连接池
	 * 
	 * @param pool
	 * @param redis
	 */
	public static void returnResource(JedisPool pool, Jedis redis) {
		if (redis != null) {
//			pool.returnResource(redis);
			redis.close();
		}
	}

	/**
	 * 获取数据
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		String value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			// 释放redis对象
//			pool.returnBrokenResource(jedis);
			jedis.close();
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * 存储数据
	 * 
	 * @param key
	 * @return
	 */
	public static void set(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);

		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
	}

	/**
	 * 描述:元素递增
	 * 
	 * @param key
	 * @author shiqingshan
	 * @date 20160420
	 * 
	 */
	public static void incr(String key) {
		if (StringUtils.isEmpty(key)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.incr(key);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
	}

	/**
	 * 保存key，并且设置过期时间
	 * 
	 * @param key
	 * @param time
	 *            秒
	 * @param value
	 */
	public static void setex(String key, int time, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.setex(key, time, value);
			// jedis.expire(key, time);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
	}

	/**
	 * 保存key，并且设置过期时间
	 * 
	 * @param key
	 * @param time
	 *            秒
	 * @param value
	 */
	public static void lpush(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, value);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
	}

	public static List<String> getAllList(String key) {
		List<String> value = new ArrayList<String>();
		if (StringUtils.isBlank(key)) {
			return value;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.lrange(key, 0, -1);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 获取list中最后一条
	 * 
	 * @param key
	 * @return
	 */
	public static String getListLastOne(String key) {
		String value = null;
		if (StringUtils.isBlank(key)) {
			return value;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			List<String> list = jedis.lrange(key, 0, 0);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 获取用户最后一个订单号
	 * 
	 * @author liuyuanfu 20141201
	 * @param key
	 * @return
	 */
	public static List<String> getLastOrder(String key) {
		List<String> value = new ArrayList<String>();
		if (StringUtils.isBlank(key)) {
			return value;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.lrange(key, 0, 0);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 设置集合的多个值，放入Set集合
	 * 
	 * @param key
	 * @param value
	 */
	public static void sadd(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.sadd(key, value);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
	}

	/**
	 * 设置Map值
	 * 
	 * @param key
	 * @param value
	 */
	public static void hset(String key, String field, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value) || StringUtils.isEmpty(field)) {
			return;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hset(key, field, value);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
	}

	/**
	 * 从Map中获取value
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public static String hget(String key, String field) {
		String value = null;
		if (StringUtils.isEmpty(key) || StringUtils.isEmpty(field)) {
			return value;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.hget(key, field);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return value;
	}

	/**
	 * 获取Map值
	 * 
	 * @param key
	 * @param value
	 */
	@SuppressWarnings("rawtypes")
	public static Map hgetAll(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Map map = jedis.hgetAll(key);
			return map;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 获取集合类的值
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Set getSmembers(String key) {
		if (StringUtils.isEmpty(key)) {
			return null;
		}
		Set value = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.smembers(key);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * 从List中删除指定的value
	 * 
	 * @param key
	 * @param value
	 */
	public static Long lrem(String key, String value) {
		Long ret = 0L;
		if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
			return ret;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			ret = jedis.lrem(key, 0, value); // 从List中移除所有指定value的内容
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return ret;
	}

	/**
	 * 给制定频道发布消息
	 * 
	 * @param channel
	 * @param message
	 * @return
	 */
	public static Long publish(String channel, String message) {
		Long ret = 0L;
		if (StringUtils.isEmpty(channel) || StringUtils.isEmpty(message)) {
			return ret;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			ret = jedis.publish(channel, message);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return ret;
	}

	/**
	 * 删除一个key
	 * 
	 * @param key
	 * @param value
	 */
	public static Long del(String key) {
		Long ret = 0L;
		if (StringUtils.isEmpty(key)) {
			return ret;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			ret = jedis.del(key);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return ret;
	}

	/**
	 * 获取keys
	 * 
	 * @author Arno
	 * @param pattern
	 * @return
	 */
	public static Set<String> getKeys(String pattern) {
		Set<String> keys = new HashSet<String>();
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			keys = jedis.keys(pattern);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return keys;
	}

	/**
	 * 删除keys
	 * 
	 * @author Arno
	 * @param pattern
	 * @return
	 */
	public static long delKeys(String... keys) {
		JedisPool pool = null;
		Jedis jedis = null;
		long i = 0L;
		try {
			pool = getPool();
			jedis = pool.getResource();
			i = jedis.del(keys);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return i;
	}

	/**
	 * 获取key
	 * 
	 * @author Arno
	 * @param pattern
	 * @return
	 */
	public static String getsetKey(String key, String value) {
		if (StringUtils.isEmpty(value) || StringUtils.isEmpty(value)) {
			return null;
		}

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			String oldValue = jedis.getSet(key, value);
			return oldValue;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 针对key增加指定value
	 * 
	 * @param key
	 * @param value
	 * @return key+value
	 * @author shiqingshan
	 */
	public static Long incrby(String key, Long value) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long newValue = jedis.incrBy(key, value);
			return newValue;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;

	}

	/**
	 * 针对key减去指定value
	 * 
	 * @param key
	 * @param value
	 * @return key-value
	 * @author shiqingshan
	 */
	public static Long decrby(String key, Long value) {

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long newValue = jedis.decrBy(key, value);
			return newValue;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 向对应sortedSet里插入指定权重的元素
	 * 
	 * @param sortedSetName
	 * @param elementScore
	 *            元素权重
	 * @param elementname
	 *            元素名
	 * @return
	 */
	public static Long sortedSetAdd(String sortedSetName, double elementScore, String elementname) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long result = jedis.zadd(sortedSetName, elementScore, elementname);
			return result;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 删除指定sortedSet的元素
	 * 
	 * @param sortedSetName
	 * @param elementname
	 *            元素组
	 * @return
	 */
	public static Long sortedSetRemove(String sortedSetName, String... elementname) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long result = jedis.zrem(sortedSetName, elementname);
			return result;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 取得指定sortedSet的全部元素（默认升序）
	 * 
	 * @param sortedSetName
	 * @return
	 */
	public static Set<String> sortedRang(String sortedSetName) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Set<String> result = jedis.zrange(sortedSetName, 0, -1);
			return result;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 取得指定sortedSet的全部元素（默认升序）
	 * 
	 * @param sortedSetName
	 * @param start
	 *            其实index
	 * @param end
	 *            终止index
	 * @return
	 */
	public static Set<String> sortedRang(String sortedSetName, Long start, Long end) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Set<String> result = jedis.zrange(sortedSetName, start, end);
			return result;
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return null;
	}

	/**
	 * 活动指定sortedSet的某元素的排名 未找到指定元素时返回-1
	 * 
	 * @param sortedSetName
	 * @param elementName
	 * @return
	 */
	public static Long getRang(String sortedSetName, String elementName) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long rank = jedis.zrank(sortedSetName, elementName);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return -1L;
	}

	/**
	 * 在listName对应list的头部添加字符串元素
	 * 
	 * @param listName
	 * @param value
	 * @return
	 */
	public static long lpush(String listName, String... value) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long rank = jedis.lpush(listName, value);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return -1L;
	}

	/**
	 * 从key对应listName中删除count个和value相同的元素 count>0时，按从头到尾的顺序删除
	 * count<0时，按从尾到头的顺序删除 count=0时，删除全部
	 * 
	 * @param listName
	 * @param count
	 * @param value
	 * @return
	 */
	public static long lrem(String listName, long count, String value) {

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long rank = jedis.lrem(listName, count, value);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return -1L;

	}

	/**
	 * 对特定setName的set增加一个或多个值，返回是增加元素的个数。 注意：对同一个member多次add，set中只会保留一份。
	 * 
	 * @param setName
	 * @param members
	 * @return
	 */
	public static long sadd(String setName, String... members) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long rank = jedis.sadd(setName, members);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return -1L;
	}

	/**
	 * 判断值是否是set的member。 如果值是set的member返回true，否则，返回false
	 * 
	 * @param setName
	 * @param member
	 * @return
	 */
	public static boolean sismember(String setName, String member) {
		JedisPool pool = null;
		Jedis jedis = null;
		Boolean exists = false;
		try {
			pool = getPool();
			jedis = pool.getResource();
			exists = jedis.sismember(setName, member);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return exists;
	}

	/**
	 * 移除一个或多个member
	 * 
	 * @param setName
	 * @param members
	 * @return
	 */
	public static long srem(String setName, String... members) {
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			Long rank = jedis.srem(setName, members);
			if (rank == null) {
				return -1L;
			} else {
				return rank;
			}
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return -1L;
	}

	public static Set<String> keys(String pattern) {
		JedisPool pool = null;
		Jedis jedis = null;
		Set<String> value = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.keys(pattern);
		} catch (Exception e) {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			returnResource(pool, jedis);
		}
		return value;
	}

}
